<template>
	<view class="page content-box">
		<cover-image src="/static/logo_img.png"></cover-image>
		<view class="logo-text">
			<image class="logo-text1" src="/static/logo_text1.png"></image>
		</view>


		<view class="user-info">
			<button style="display: none;" class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<image class="avatar" :src="avatarUrl"></image>
			</button>
			<view class="petName">
				<view class="petName_name">昵称</view>
				<input type="nickname" v-model="nickName" class="weui-input" placeholder="请输入昵称"
					@input="onInputNickname" />
			</view>
			<button class="loginBtn" form-type="submit" @tap="toIndex">加 入 房 间</button>
		</view>
	</view>
</template>

<script>
	import config from "@/utils/config.js";
	import Vue from 'vue'

	export default {
		data() {
			return {
				openId: "",
				userInfo: {},
				code: "",
				roomId: 5,
				nickName: '微信用户',
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
			}
		},
		onReady() {
			this.getTopValue();
		},
		onLoad(query) {
			console.log(query);
			var that = this;
			if (query.q) {
				const q = decodeURIComponent(query.q);
				var room = q.split('=')[1];
				if (room) {
					this.roomId = parseInt(room);
					uni.setStorage({
						key: 'roomId',
						data: this.roomId
					});
				}
			} else {
				uni.getStorage({
					key: "roomId",
					success(res) {
						if (res.data) {
							that.roomId = res.data;
						}
					}
				})
			}

			uni.getStorage({
				key: "openId",
				success(res) {
					if (res.data) {
						that.openId = res.data;
					}
				}
			})

			uni.getStorage({
				key: "avatarUrl",
				success(res) {
					if (res.data) {
						that.avatarUrl = res.data;
					}
				}
			})

			uni.getStorage({
				key: "nickName",
				success(res) {
					if (res.data) {
						that.nickName = res.data;
					}
				}
			})

		},
		methods: {
			// 选择头像
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail;
				this.avatarUrl = avatarUrl;
			},
			// 输入框输入或选择微信昵称
			onInputNickname(event) {
				this.nickName = event.target.value;
			},
			getTopValue() {
				uni.getSystemInfo({
					success: (res) => {
						getApp().globalData.topValue = `${res.safeArea.top}px`;
						console.log(getApp().globalData.topValue);
					}
				})
			},
			toIndex() {
				uni.login({
					success: (loginRes) => {
						this.code = loginRes.code;
						this.toLogin();
					},
					complete(res) {
						console.log(res);
					}
				})
			},
			toLogin() {
				this.$api.login({
					code: this.code,
					nickName: this.nickName,
					avatarUrl: this.avatarUrl,
					roomId: this.roomId,
					openId: this.openId
				}).then(res => {
					console.log(res);
					if (res.isSucc) {
						Vue.prototype.$room = res.res.roomInfo;
						Vue.prototype.$user = res.res.wxUserInfo;

						uni.setStorage({
							key: 'openId',
							data: res.res.wxUserInfo.openId
						});


						uni.setStorage({
							key: 'avatarUrl',
							data: this.avatarUrl
						});

						uni.setStorage({
							key: 'nickName',
							data: this.nickName
						});

						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
				}, err => {
					console.log(err);
				})
			},
			getUserInfo() {
				Vue.prototype.$user = this.userInfo;
			},
			jumpToHome() {
				uni.redirectTo({
					url: '/pages/index/index?roomId=' + this.roomId
				})
			}
		}
	}
</script>

<style>
	/* cover-image {
	    width: 100%;
	    height: 100vh;
	  } */

	.content-box {
		width: 100vw;
		/* height: 100vh; */
		/* background-color: white;*/
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.logo-text {
		/* width: 338rpx;
		height: 82rpx; */
		position: absolute;
		bottom: 50rpx;
		/* left: 206rpx; */
	}

	.logo-text1 {
		width: 132rpx;
		height: 132rpx;
	}

	.user-info {
		position: absolute;
		bottom: 200rpx;
	}

	.avatar-wrapper {
		width: 60px;
		height: 60px;
		padding: 0;

	}

	.avatar {
		width: 60px;
		height: 60px;
	}

	.petName {
		display: flex;
		align-items: center;
		padding: 10px;
		margin-top: 20px;
		border-top: 1px solid #e8e8e8;
		border-bottom: 1px solid #e8e8e8;
		margin-bottom: 30px;

	}

	.petName_name {
		margin-right: 30px;
	}

	.loginBtn {
		color: #fff;
		border-radius: 35px;
		background: #226be4;
	}
</style>